from rest_framework.response import Response
import six
from rest_framework import status


class ResultCode:
    # 响应成功，状态码200
    CODE_SUCCESS = '000000'
    MSG_SUCCESS = '成功'
    # token失效或权限认证错误，状态码401
    CODE_AUTH_ERROR = '000001'
    MSG_AUTH_ERROR = 'token失效或权限认证错误'
    # 业务上的错误
    CODE_BUSSINESS_ERROR = '000002'
    MSG_BUSSINESS_ERROR = ''
    # 服务器内部错误，状态码500
    CODE_SERVER_ERROR = '000003'
    MSG_SERVER_ERROR = '网络操作失败，请稍后重试'
    # 未发现接口
    CODE_NOT_FOUND_ERROR = '000004'
    MSG_NOT_FOUND_ERROR = '服务器没有此接口'
    # 未知错误
    CODE_UNKNOWN_ERROR = '000005'
    MSG_UNKNOWN_ERROR = '未知错误'
    # 拒绝访问
    CODE_REJECT_ERROR = '000006'
    MSG_REJECT_ERROR = '拒绝访问'
    # 拒绝访问
    CODE_METHOD_ERROR = '000007'
    MSG_METHOD_ERROR = '请求方法错误'
    # 缺少参数
    CODE_PARAMETER_ERROR = '000008'
    MSG_PARAMETER_ERROR = '参数错误'
    # 未找到相关数据
    CODE_NO_MATCHING_ERROR = '000009'
    MSG_NO_MATCHING_ERROR = '未找到相关数据'


class BaseResponse(Response):
    def __init__(self, code=ResultCode.CODE_SUCCESS, message=ResultCode.MSG_SUCCESS, data=None,
                 _status=status.HTTP_200_OK,
                 template_name=None, headers=None, exception=False, content_type='application/json'):
        super().__init__(None, status=_status)
        if data is None:
            data = {}
        self._code = code
        self._message = message
        self._data = data

        self.data = {"code": code, "message": message, "data": data}
        # self.template_name = template_name
        # self.exception = exception
        # self.content_type = content_type
        #

        if headers:
            for name, value in six.iteritems(headers):
                self[name] = value

    @property
    def code(self):
        return self._code

    @code.setter
    def code(self, value):
        self._code = value

    @property
    def message(self):
        return self._message

    @message.setter
    def message(self, value):
        self._message = value

    @property
    def data(self):
        return self._data

    @data.setter
    def data(self, value):
        self._data = value


def isStrNull(target):
    return target is None or not isinstance(target, str) or target == ''
